*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	This program creates a file with NYC school characteristics
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
args bw
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

**	enrollment characteristics

*	load enrollment file

	//this is a student by year file
	use "${cleandata}demo_all_years_all_grade", clear

	//restrict to students in the grade range specified
	destring grade, replace force
	keep if grade == 9
	keep if inrange(year, 2013, 2015)

	//destring school codes and student ID
	replace sch = subinstr(sch,"B","1",.)
	replace sch = subinstr(sch,"K","2",.)
	replace sch = subinstr(sch,"M","3",.)
	replace sch = subinstr(sch,"Q","4",.)
	replace sch = subinstr(sch,"R","5",.)
	replace sch = subinstr(sch,"X","6",.)
	destring sch, replace

	//collapse
	egen tag = tag(sch year)
	keep if tag
	keep sch year

**	identify screened schools

	preserve
		do "${code_build}b_screened" `bw'
	restore

	preserve
		clear
		set obs 1
		gen year = .
		forval y =2012/2014{
			append using "${builddata}screened_dbns_`y'", gen(new)
			replace year = `y' if new
			drop new
		}
		drop in 1
		rename app_dbn_last_4 sch
		rename scr4 screened
		keep sch screened year
		replace sch = subinstr(sch,"B","1",.)
		replace sch = subinstr(sch,"K","2",.)
		replace sch = subinstr(sch,"M","3",.)
		replace sch = subinstr(sch,"Q","4",.)
		replace sch = subinstr(sch,"R","5",.)
		replace sch = subinstr(sch,"X","6",.)
		destring sch, replace
		drop if mi(sch)
		replace year = year + 1
		tempfile screened_schs
		save "`screened_schs'"
	restore
	merge m:1 sch year using "`screened_schs'", keep(1 3) nogen
	replace screened = 0 if mi(screened)
	replace screened = 1 if inlist(sch,2430,2449,3475,3485,3692,4687,5605,6445,6696) //specialized HS

***	save

	save "${builddata}${city}_schoolcovs", replace
